import QtQuick 1.0
import com.nokia.symbian 1.0 // Symbian components
import com.nokia.extras 1.0 // Extras

import SocketDemo 1.0

Page {
    id: mainPage

    // *******************************************************
    // NOTE: Setup for are you Server or Client
    property bool isServer : false
    // *******************************************************

    Text {
        id: title
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.topMargin: 10
        //text: "Client & Server Sockets"
        text: isServer ? "Server Socket" : "Client Socket"
        color: platformStyle.colorNormalLight
        font.pixelSize: 20
    }

    Flickable {
        id: flickable

        property string logText;

        anchors.margins: 10
        anchors.top: title.bottom
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.right: parent.right

        clip: true

        contentHeight: text.height

        Text {
            id:text
            color: "white"
            width: flickable.width
            wrapMode: Text.WrapAtWordBoundaryOrAnywhere
            text: flickable.logText
            font.pixelSize: width * 0.05
        }
    }



    Component.onCompleted: {
        if (isServer)
            serverSocket.start();
        else
            clientSocket.start();
    }

    ClientSocket {
        id: clientSocket

        udpOnly: true;

        onConnected: {
            console.log("Client try to connect to server "+address);
        }

        onClientLog: {
            infobanner.close();
            infobanner.text = log;
            infobanner.open();
            flickable.logText += "\n"+log;
        }
    }

    ServerSocket {
        id: serverSocket

        udpOnly: true;

        onClientConnected: {
            console.log("Client connected to server "+address+" port "+port);
        }

        onServerLog: {
            infobanner.close();
            infobanner.text = log;
            infobanner.open();
            flickable.logText += "\n"+log;

        }

    }

    InfoBanner {
        id: infobanner
    }


}
